package com.topscomm.base.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;

/**
 * @description: 工具类
 * @author: chengengwei
 * @date: 2021-08-04
 * @modify:
 */
@Slf4j
public class BaseUtils {

    /**
     * @description: 解析CLOB类型数据
     * @author: chengengwei
     * @date: 2021-08-04
     * @modify:
     */
    public static String ClobToString(Clob clob) {
        String result = "";
        if (clob != null) {
            Reader reader = null;
            BufferedReader br = null;
            try {
                reader = clob.getCharacterStream();
                br = new BufferedReader(reader);
                String s = br.readLine();
                StringBuffer sb = new StringBuffer();
                while (s != null) {
                    sb.append(s);
                    s = br.readLine();
                }
                result = sb.toString();
            } catch (SQLException e) {
                e.printStackTrace();
                log.error(e.getMessage());
            } catch (IOException e) {
                e.printStackTrace();
                log.error(e.getMessage());
            } finally {
                close(reader);
                close(br);
            }
        }
        return result;
    }

    private static <T extends java.io.Closeable> void close(T t) {
        try {
            if (t != null) {
                t.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

}
